﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Autorent.Desktop.Model;
using Autorent.Desktop.Model.AutorentMngrServiceReference;

namespace Autorent.Desktop.Modules.Offices.Services
{
    public class OfficesRepository :AutorentEntitiesContext, IOfficesRepository
    {
       
        public ObservableCollection<Office> GetOffices()
        {
            LoadRelatedEntities();
            try
            {
                return new ObservableCollection<Office>(Context.Offices);
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while connecting service", ex.InnerException);
            }            
        }


        /// <summary>
        /// Loads related entities for each office in collection
        /// </summary>
        private void LoadRelatedEntities()
        {
            try
            {
                foreach (Office office in Context.Offices)
                {
                    if (office != null)
                    {
                        Context.LoadProperty(office, "Street");
                        if (office.Street != null)
                        {
                            Context.LoadProperty(office.Street, "City");
                            if (office.Street.City != null)
                            {
                                Context.LoadProperty(office.Street.City, "Region");
                                if (office.Street.City.Region != null)
                                {
                                    Context.LoadProperty(office.Street.City.Region, "Country");
                                }
                            }
                        }

                        Context.LoadProperty(office, "AutoOrders");
                    }
                }
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while loading offices properties", ex.InnerException);
            }
        }



        public void AddOffice(Office office)
        {
            Context.AddToOffices(office);
            try
            {
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while connecting service", ex.InnerException);
            }
        }


        public void UpdateOffice(Office office)
        {
            try
            {
                Context.UpdateObject(Context.Offices.Where<Office>(o => o.Office_ID.Equals(office.Office_ID)).First());
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while updating object", ex.InnerException);
            }
        }


        public void DeleteOffice(Office office)
        {
            try
            {
                Context.DeleteObject(Context.Offices.Where<Office>(o => o.Office_ID.Equals(office.Office_ID)).First());
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while deleting object", ex.InnerException);
            }
        }
    }
}
